package com.yeyv.net.Web.Control.Anonymous;

import cn.hutool.core.lang.hash.Hash;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yeyv.net.Annotation.ControllerApi.AnonController;
import com.yeyv.net.UsefulEntity.Constant;
import com.yeyv.net.UsefulEntity.Resp;
import com.yeyv.net.Web.Mapper.GameAutoAuthMapper;
import com.yeyv.net.Web.Service.GameAutoAuthService;
import com.yeyv.net.Web.Service.GameCdkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@AnonController("/autoAuth")
public class GameAutoAuthCntroller {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private GameAutoAuthService gameAutoAuthService;

    @Autowired
    private GameCdkService gameCdkService;

    @DS("game_1")
    @GetMapping("/lt")
    public Resp<String> game_lt(@RequestParam String user,
                                @RequestParam String cdk) {
        if (StringUtils.isEmpty(user)) {
            return Resp.error("账号不能为空！");
        }
        if (StringUtils.isEmpty(cdk)) {
            return Resp.error("CDK不能为空！");
        }
        var role = gameAutoAuthService.getLTRole(user);
        if (role == null) {
            return Resp.error("授权错误：账号不存在或暂未创建角色！");
        }
        if (gameCdkService.validateCdk("SGLT", cdk)) {
            return Resp.error("授权错误：CDK ERROR");
        }
        log.info("用户授权 => game 三国雷霆 user {} , cdk {}", user, cdk);
        var result = gameAutoAuthService.autoAuthLT(user, role, cdk);
        return Resp.success(result);
    }

    @DS("app_sszg_1")
    @GetMapping("/sszg")
    public Resp<String> game_sszg(@RequestParam String user,
                                  @RequestParam Integer qu,
                                  @RequestParam String cdk
    ) {
        if (StringUtils.isEmpty(user)) {
            return Resp.error("账号不能为空！");
        }
        if (StringUtils.isEmpty(cdk)) {
            return Resp.error("CDK不能为空！");
        }
        if (!gameCdkService.validateCdk("SSZG", cdk)) {
            return Resp.error("授权错误：CDK ERROR");
        }
        var result = switch (qu) {
            case 1 -> {
                log.info("用户授权 => game:三国雷霆 , qu:1 , user:{} , cdk {}", user, cdk);
                yield gameAutoAuthService.autoAuthSSZG1(user, cdk);
            }
            case 2 -> {
                log.info("用户授权 => game:三国雷霆 , qu:2 , user:{} , cdk {}", user, cdk);
                yield gameAutoAuthService.autoAuthSSZG2(user, cdk);
            }
            default -> "区服错误！";
        };
        return Resp.success(result);
    }
}
